package org.barad.architecture.analyzer.archpad.cell.editor;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.AbstractCellEditor;

import org.barad.architecture.analyzer.archpad.component.ArchPadComponent;
import org.jgraph.JGraph;
import org.jgraph.graph.DefaultGraphCellEditor;
import org.jgraph.graph.GraphCellEditor;


/**
 * This class is an editor for the ArchPad component stored
 * in a JGraph cell.
 * 
 * @author svetoslavganov@gmail.com (Svetoslav R. Ganov)
 *
 */
public class JGraphUserObjectEditor extends DefaultGraphCellEditor {
	private ComponentEditor componentEditor;
	
	public static final long serialVersionUID = 1L;

	/**
	 * @see DefaultGraphCellEditor#createGraphCellEditor()
	 */
	@Override
	protected GraphCellEditor createGraphCellEditor() {
		return new RealCellEditor();
	}

	/**
	 * Utility editor for editing JGraph cells via a 
	 * specific {@link ComponentEditor} instance.
	 */
	class RealCellEditor extends AbstractCellEditor implements GraphCellEditor {
		public static final long serialVersionUID = 1L;

		/**
		 * Creates a new {@link RealCellEditor} instance.
		 */
		public RealCellEditor() {
			componentEditor = new ComponentEditor(this);
			componentEditor.setSize(new Dimension(280, 280));
			componentEditor.setPreferredSize(componentEditor.getSize());
		}

		/**
		 * Returns the editor of the user object, i.e. in this case
		 * an ArchPad component.
		 * 
		 * @param jgraph The JGraph instance.
		 * @param value The user object stored in a JGraph cell. 
		 */
		public Component getGraphCellEditorComponent(JGraph graph, Object value, 
				boolean isSelected) {
			Object component = graph.getModel().getValue(value);
			componentEditor.setComponent((ArchPadComponent)component);
			return componentEditor;
		}

		/**
		 * Returns the rich text value to be stored in the user object.
		 */
		public Object getCellEditorValue() {
			return componentEditor.getComponent();
		}
	}
}

